package com.xiaoyu.shenyou.shenyouv1.services.impl;

import cn.hutool.core.util.ObjectUtil;
import com.xiaoyu.shenyou.shenyouv1.common.roomname.RoomName;
import com.xiaoyu.shenyou.shenyouv1.mapper.RecordMapper;
import com.xiaoyu.shenyou.shenyouv1.entity.RecordEntity;
import com.xiaoyu.shenyou.shenyouv1.services.RecordService;
import com.xiaoyu.shenyou.shenyouv1.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    @Async
    public void insertRecord(String killerRoom, int games) {
        //取出上条记录
        RecordEntity last = Optional.ofNullable(recordMapper.selectRecordLast()).orElseGet(() ->
                new RecordEntity().setRoom_name("初始化")
                        .setGames(0)
                        .setHuoyan_win(0)
                        .setNver_win(0)
                        .setGaolao_win(0)
                        .setHuaguo_win(0)
                        .setShuilian_win(0)
                        .setWuzhi_win(0)
                        .setLiusha_win(0)
                        .setBajiao_win(0)
                        .setCreate_time(LocalDateTime.now())
        );
        last.setRoom_name(killerRoom)
                .setGames(games)
                .setHuoyan_win(RoomName.huoyan.equals(killerRoom) ? 0 : last.getHuoyan_win() + 1)
                .setNver_win(RoomName.nver.equals(killerRoom) ? 0 : last.getNver_win() + 1)
                .setGaolao_win(RoomName.gaolao.equals(killerRoom) ? 0 : last.getGaolao_win() + 1)
                .setHuaguo_win(RoomName.huaguo.equals(killerRoom) ? 0 : last.getHuaguo_win() + 1)
                .setShuilian_win(RoomName.shuilian.equals(killerRoom) ? 0 : last.getShuilian_win() + 1)
                .setWuzhi_win(RoomName.wuzhi.equals(killerRoom) ? 0 : last.getWuzhi_win() + 1)
                .setLiusha_win(RoomName.liusha.equals(killerRoom) ? 0 : last.getLiusha_win() + 1)
                .setBajiao_win(RoomName.bajiao.equals(killerRoom) ? 0 : last.getBajiao_win() + 1)
                .setCreate_time(LocalDateTime.now());
        recordMapper.insertRecord(last);
        //读取100条历史记录放去缓存
        Optional.ofNullable(recordMapper.slectRecordsBySzie(100)).ifPresent(records -> {
            redisTemplate.opsForValue().set("records", records);
            redisTemplate.expire("records", 5, TimeUnit.MINUTES);
        });
    }

    @Override
    public List<RecordEntity> queryRecords(int size) {
        String currentId = BaseContext.getCurrentId();
        redisTemplate.opsForValue().set(currentId, currentId);
        redisTemplate.expire(currentId, 5, TimeUnit.SECONDS);
        List<RecordEntity> records = (List<RecordEntity>) redisTemplate.opsForValue().get("records");
        if (ObjectUtil.isNull(records)) {
            List<RecordEntity> recordEntities = recordMapper.slectRecordsBySzie(100);
            redisTemplate.opsForValue().set("records", recordEntities);
            return recordEntities;
        }
        return records;
    }
}
